package org.gap.wizards.views;

import java.util.Stack;

import net.slashie.libjcsi.textcomponents.TextInformBox;

import org.gap.jseed.injection.annotation.Singleton;
import org.gap.wizards.views.ViewPlatform.Color;
import org.gap.wizards.world.TimerState;

@Singleton
public class MessageOutput implements View {
	public static final String CLEAR_STRING = "                                                            ";
	private final ViewPlatform console;
	private Stack<String> messages;
	private final TimerState timerState;
	private int timeOfMessageAdd;

	public MessageOutput(ViewPlatform console, TimerState timerState) {
		this.console = console;
		this.timerState = timerState;
		messages = new Stack<String>();
		messages.add(CLEAR_STRING);
		messages.add(CLEAR_STRING);
		messages.add(CLEAR_STRING);
		messages.setSize(3);
	}

	public void addMessage(String message) {
		timeOfMessageAdd = timerState.getTime();
		messages.insertElementAt(message, 0);
		messages.trimToSize();
		print();
	}

	@Override
	public void draw() {
		print();
	}

	private void print() {
		if (timeOfMessageAdd == timerState.getTime()) {
			console.print(5, 0, Color.GRAY, trim(messages.get(2) + CLEAR_STRING));
			console.print(5, 1, Color.GRAY, trim(messages.get(1) + CLEAR_STRING));
			console.print(5, 2, Color.WHITE, trim(messages.get(0) + CLEAR_STRING));
			console.refresh();
		}
	}

	private String trim(String string) {
		if (string.length() > 115) { return string.substring(0, 114); }
		return string;
	}
}
